; (function () {
    // return
    /**
 * @param {string} s
 * @param {string} p
 * @return {number[]}
 */
    var findAnagrams = function (s, p) {
        let left = 0
        let right = 0
        let arr = []
        let res = new Array(26).fill(0);
        for (let index = 0; index < p.length; index++) {
            ++res[p[index].charCodeAt() - 'a'.charCodeAt()];
        }



        while (left < s.length) {

            while ((right - left) < p.length && right < s.length) {

                if ((right + 1 - left) == p.length) {

                    let str = s.slice(left, right + 1)
                    let strRes = new Array(26).fill(0);
                    for (let index = 0; index < str.length; index++) {
                        ++strRes[str[index].charCodeAt() - 'a'.charCodeAt()];
                    }
                    if (strRes.toString() == res.toString()) arr.push(left)
                    break
                }
                right++
                if (right == s.length) {
                    right--
                    break

                }
            }
            left++
        }
        return arr

    };
    console.log(findAnagrams('kzexfqiudtwvpjnhgocasbmlrykzex', 'ykzexfqiudtwvpjnhgocasbmlr'));
})();

// 官方解
; (function () {
    return
    var findAnagrams = function (s, p) {
        const sLen = s.length, pLen = p.length;

        if (sLen < pLen) {
            return [];
        }

        const ans = [];
        const sCount = new Array(26).fill(0);
        const pCount = new Array(26).fill(0);
        for (let i = 0; i < pLen; ++i) {
            ++sCount[s[i].charCodeAt() - 'a'.charCodeAt()];
            ++pCount[p[i].charCodeAt() - 'a'.charCodeAt()];
        }


        if (sCount.toString() === pCount.toString()) {
            ans.push(0);
        }

        for (let i = 0; i < sLen - pLen; ++i) {
            --sCount[s[i].charCodeAt() - 'a'.charCodeAt()];
            ++sCount[s[i + pLen].charCodeAt() - 'a'.charCodeAt()];

            if (sCount.toString() === pCount.toString()) {
                ans.push(i + 1);
            }
        }

        return ans;
    };
    findAnagrams('kzexfqiudtwvpjnhgocasbmlrykzex', 'ykzexfqiudtwvpjnhgocasbmlryy')
})();

// 最快解
; (function () {
    // return
    /**
 * @param {string} s
 * @param {string} p
 * @return {number[]}
 */
    var findAnagrams = function (s, p) {
        const cnts = Array(26).fill(0);
        const arrp = Array(26).fill(0);
        const arrEqual = (a, b) => {
            for (let i = 0; i < 26; i++) {
                if (a[i] != b[i]) return false;
            }
            return true;
        }
        //p
        const ls = s.length, lp = p.length;
        for (let i = 0; i < lp; i++) arrp[p.charCodeAt(i) - 'a'.charCodeAt()]++;

        //s
        const res = [];
        for (let i = 0; i < ls; i++) {
            // 滑块右边的加一个字符,对应字符个数加一个
            cnts[s.charCodeAt(i) - 'a'.charCodeAt()]++;
            if (i >= lp - 1) {
                if (arrEqual(cnts, arrp)) res.push(i - lp + 1);
                // 滑块左边的字符减一个,对应的个数减一个
                cnts[s.charCodeAt(i - lp + 1) - 'a'.charCodeAt()]--;
            }
        }
        return res;

    };

})();